﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace WooEngine.UI.Base
{

	public enum EventTriggerListenerType{
		onClick,
		onDown,
		onEnter,
		onExit,
		onUp,
		onSelect,
		onUpdateSelect,
		onBeginDrag,
		onEndDrag,
		onDrag
	}

	public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
		public delegate void VoidDelegate (GameObject go, BaseEventData data);
		public VoidDelegate onClick;
		public VoidDelegate onDown;
		public VoidDelegate onEnter;
		public VoidDelegate onExit;
		public VoidDelegate onUp;
		public VoidDelegate onSelect;
		public VoidDelegate onUpdateSelect;
		public VoidDelegate onDrag;
		public VoidDelegate onEndDrag;
		public VoidDelegate onBeginDrag;
		public ScrollRect scrollRect;
		public ScrollRect scrollRect2;

		[SerializeField]
		private bool isDrag = false;
		[SerializeField]
        private bool isCanUse = false;

        public void On()
        {
            isCanUse = true;
        }

        public void Off()
        {
            isCanUse = false;
        }

        static public EventTriggerListener Get (GameObject go)
		{
			EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
			if (listener == null) 
				listener = go.AddComponent<EventTriggerListener>();
			return listener;
		}

        static public void Del(GameObject go)
        {
            EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
            if (listener != null)
                GameObject.Destroy(listener);
        }


        public override void OnBeginDrag (PointerEventData eventData)
		{
            if (!isCanUse)
                return;
			if (scrollRect)
				scrollRect.OnBeginDrag (eventData);
			if(scrollRect2)
				scrollRect2.OnBeginDrag(eventData);
			isDrag = true;
			if(onBeginDrag != null) 	
				onBeginDrag(gameObject, eventData);
		}

		public override void OnEndDrag (PointerEventData eventData)
		{
            if (!isCanUse)
                return;
            if (scrollRect)
				scrollRect.OnEndDrag (eventData);
			if (scrollRect2)
				scrollRect2.OnEndDrag(eventData);
			isDrag = false;
			if(onEndDrag != null) 	
				onEndDrag(gameObject, eventData);
		}

		public override void OnDrag (PointerEventData eventData)
		{
            if (!isCanUse)
                return;
            if (scrollRect)
				scrollRect.OnDrag (eventData);
			if (scrollRect2)
				scrollRect2.OnDrag(eventData);
			if (onDrag != null) 	
				onDrag(gameObject, eventData); 

        }

		public override void OnPointerClick(PointerEventData eventData)
		{
            if (!isCanUse)
                return;
            if (isDrag)
				return;
			if(onClick != null) 	
				onClick(gameObject, eventData);
		}

		public override void OnPointerDown (PointerEventData eventData){
            if (!isCanUse)
                return;
            if (onDown != null)
				onDown(gameObject, eventData);
		}

		public override void OnPointerEnter (PointerEventData eventData){
            if (!isCanUse)
                return;
            if (onEnter != null)
				onEnter(gameObject, eventData);
		}

		public override void OnPointerExit (PointerEventData eventData){
            if (!isCanUse)
                return;
            if (onExit != null) 
				onExit(gameObject, eventData);
		}

		public override void OnPointerUp (PointerEventData eventData){
            if (!isCanUse)
                return;
            if (onUp != null) 
				onUp(gameObject, eventData);
		}

		public override void OnSelect (BaseEventData eventData){
            if (!isCanUse)
                return;
            if (onSelect != null) 
				onSelect(gameObject, eventData);
		}

		public override void OnUpdateSelected (BaseEventData eventData){
            if (!isCanUse)
                return;
            if (onUpdateSelect != null)
				onUpdateSelect(gameObject, eventData);
		}


		public static void AddListenerCSharp(EventTriggerListenerType type, VoidDelegate func, GameObject go){
			EventTriggerListener listener = EventTriggerListener.Get (go);
            switch (type) {
			case EventTriggerListenerType.onClick:
				{
					listener.onClick += func;
				}
				break;
			case EventTriggerListenerType.onDown:
				{
					listener.onDown += func;
				}
				break;
			case EventTriggerListenerType.onEnter:
				{
					listener.onClick += func;
				}
				break;
			case EventTriggerListenerType.onExit:
				{
					listener.onExit += func;
				}
				break;
			case EventTriggerListenerType.onSelect:
				{
					listener.onSelect +=func;
				}
				break;
			case EventTriggerListenerType.onUp:
				{
					listener.onUp += func;
				}
				break;
			case EventTriggerListenerType.onUpdateSelect:
				{
					listener.onUpdateSelect += func;
				}
				break;
			case EventTriggerListenerType.onDrag:
				{
					listener.onDrag += func;
				}
				break;
			case EventTriggerListenerType.onEndDrag:
				{
					listener.onEndDrag += func;
				}
				break;
			case EventTriggerListenerType.onBeginDrag:
				{
					listener.onBeginDrag += func;
				}
				break;
			}


		}

        public static void RemoveListenerCSharp(EventTriggerListenerType type, VoidDelegate func, GameObject go)
        {
            EventTriggerListener listener = EventTriggerListener.Get(go);
            switch (type)
            {
                case EventTriggerListenerType.onClick:
                    {
                        listener.onClick -= func;
                    }
                    break;
                case EventTriggerListenerType.onDown:
                    {
                        listener.onDown -= func;
                    }
                    break;
                case EventTriggerListenerType.onEnter:
                    {
                        listener.onClick -= func;
                    }
                    break;
                case EventTriggerListenerType.onExit:
                    {
                        listener.onExit -= func;
                    }
                    break;
                case EventTriggerListenerType.onSelect:
                    {
                        listener.onSelect -= func;
                    }
                    break;
                case EventTriggerListenerType.onUp:
                    {
                        listener.onUp -= func;
                    }
                    break;
                case EventTriggerListenerType.onUpdateSelect:
                    {
                        listener.onUpdateSelect -= func;
                    }
                    break;
                case EventTriggerListenerType.onDrag:
                    {
                        listener.onDrag -= func;
                    }
                    break;
                case EventTriggerListenerType.onEndDrag:
                    {
                        listener.onEndDrag -= func;
                    }
                    break;
                case EventTriggerListenerType.onBeginDrag:
                    {
                        listener.onBeginDrag -= func;
                    }
                    break;
            }


        }
    }
}